﻿using System;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLKDS.DAL;
using DevExpress.XtraEditors.DXErrorProvider;
using DevExpress.XtraEditors.Controls;
using QLKDS.DTO;

namespace QLKDS.Forms
{
    public partial class frmEmployeeLanguageEdit : XtraForm
    {
        #region Properties
        private EmployeeLanguageManager employeeManagerManager;
        private LanguageManager levelManager = new LanguageManager();
        private EmployeeManager employeeManager = new EmployeeManager();
        public EmployeeLanguageInfo EmployeeLanguage;
        #endregion

        #region Methods


        /// <summary>
        /// Clear all Textboxes on this form
        /// </summary>
        private void ClearTextBoxes()
        {
            //this.txtCode.Text = String.Empty;
            //this.txtName.Text = String.Empty;
            //this.txtDiscountRetail.Text = String.Empty;
            //this.txtSchool.Text = String.Empty;
            //this.txtDescription.Text = String.Empty;
            //this.dteCreatedDate.DateTime = DateTime.Now;
            //this.dteCreatedDate.Properties.MaxValue = DateTime.Now;
            //this.txtName.Focus();
        }
        #endregion

        #region Init form
        public frmEmployeeLanguageEdit()
        {
            InitializeComponent();
            this.employeeManagerManager = new EmployeeLanguageManager();


        }

        public void Load_info()
        {

            this.lueLanguage.Properties.DataSource = levelManager.ListLanguage();
            this.lueLanguage.Properties.DisplayMember = "Name";
            this.lueLanguage.Properties.ValueMember = "Id";

            this.lueEmployee.Properties.DataSource = employeeManager.ListEmployee();
            this.lueEmployee.Properties.DisplayMember = "Fullname";
            this.lueEmployee.Properties.ValueMember = "Id";

            this.txtDescription.Text = EmployeeLanguage.Description;
            this.txtSchool.Text = EmployeeLanguage.PlaceIssue;
            this.txtSpecialized.Text = EmployeeLanguage.LanguageLevel;
            this.dteDateIssue.DateTime = EmployeeLanguage.DateIssue;
            this.lueEmployee.EditValue = EmployeeLanguage.EmployeeId;
            this.lueLanguage.EditValue = EmployeeLanguage.LanguageId;
  
        }

        private void ccbeAuthors_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            ButtonEdit editor = (ButtonEdit)sender;
            EditorButton Button = e.Button;
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Plus)
            {
                //var form = new frmAuthorsEdit(this);
               // form.ShowDialog();
            }
        }

        private void ccbeCategory_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            ButtonEdit editor = (ButtonEdit)sender;
            EditorButton Button = e.Button;
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Plus)
            {
             //   var form = new frmCategoryEdit(this);
                //form.ShowDialog();
            }
        }



        private void frmEmployeeLanguageEdit_Load(object sender, EventArgs e)
        {
            this.ClearTextBoxes();

            this.Load_info();

         
        }
        #endregion

        #region Validate

        private void lueLanguage_EditValueChanged(object sender, EventArgs e)
        {
            if (this.lueLanguage.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                this.dxepLanguage.ClearErrors();
            }
        }
        private void lueEmployee_EditValueChanged(object sender, EventArgs e)
        {
            if (this.lueEmployee.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                this.dxepEmployee.ClearErrors();
            }
        }
        private void txtSpecialized_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtSpecialized.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                this.dxepSpecialized.ClearErrors();
            }
        }


        private void txtSchool_EditValueChanged(object sender, EventArgs e)
        {
            if (this.txtSchool.Text.Trim() == String.Empty)
            {
                //
            }
            else
            {
                this.dxepSchool.ClearErrors();
            }
        }


    


        #endregion

        #region Button_click
        private void btnClear_Click(object sender, EventArgs e)
        {
            this.ClearTextBoxes();
        }
       
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.lueLanguage.Text.Trim() == String.Empty)
            {
                this.dxepLanguage.SetError(this.lueLanguage, "Chọn tên ngoại ngữ");
                return;
            }
            else if (this.lueEmployee.Text.Trim() == String.Empty)
            {
                this.dxepEmployee.SetError(this.lueEmployee, "Chọn nhân viên");
                return;
            }
            else if (this.txtSpecialized.Text.Trim() == String.Empty)
            {
                this.dxepSpecialized.SetError(this.txtSpecialized, "Trình độ ngoại ngữ không được bỏ trống");
                    return;
            }
            else if(this.txtSchool.Text.Trim()==String.Empty)
            {
                this.dxepSchool.SetError(this.txtSchool,"Nơi cấp không được bỏ trống");
                return;
            }
            else
            {
                this.dxepSchool.ClearErrors();
                this.dxepSpecialized.ClearErrors();
                this.dxepEmployee.ClearErrors();
                this.dxepDateIssue.ClearErrors();
                this.dxepLanguage.ClearErrors();
            }


            /*****************************************************************/
            // Save employeeManager into database                   
            var employeeManager = new EmployeeLanguage();
            employeeManager.Id = EmployeeLanguage.Id;
            employeeManager.LanguageId = (int)this.lueLanguage.EditValue;
            employeeManager.EmployeeId = (int)lueEmployee.EditValue;
            employeeManager.PlaceIssue = this.txtSchool.Text.Trim();
            employeeManager.LanguageLevel = this.txtSpecialized.Text.Trim();
            employeeManager.DateIssue = this.dteDateIssue.DateTime;
            employeeManager.Description = this.txtDescription.Text.Trim();

            this.employeeManagerManager.UpdateEmployeeLanguage(employeeManager);
            // save employeeManager


            XtraMessageBox.Show("Sửa dữ liệu thành công");
            this.DialogResult = DialogResult.OK;
            this.Close();
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        #endregion


    





      

 

      

     
        


    }
}
